package fr.andromede.utilities;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;

import android.util.Log;

public class AndromedeFileReader {

    public static String InputStreamToString ( final InputStream is, final int bufferSize ) {
        final char[] buffer = new char[bufferSize];
        final StringBuilder out = new StringBuilder();
        try {
            final Reader in = new InputStreamReader(is , "UTF-8");
            try {
                for (;;) {
                    int rsz = in.read(buffer , 0 , buffer.length);
                    if (rsz < 0)
                        break;
                    out.append(buffer , 0 , rsz);
                }
            } finally {
                in.close();
            }
        } catch (UnsupportedEncodingException ex) {
            Log.e(AndromedeFileReader.class.getSimpleName() , ex.getMessage());
            ex.printStackTrace();
        } catch (IOException ex) {
            Log.e(AndromedeFileReader.class.getSimpleName() , ex.getMessage());
            ex.printStackTrace();
        }
        return out.toString();
    }
}
